

var scene,camera,renderer;

function init(){
    //建立场景对象  
    scene = new THREE.Scene();
    //建立相机对象
    camera = new THREE.PerspectiveCamera(
        90,
        document.body.clientWidth/document.body.clientHeight,
        0.1,
        100
    )
    camera.position.set(0,0,3);
    //新建渲染器对象
    renderer = new THREE.WebGLRenderer({
        antialias:true  // 优化，抗锯齿
    });
    renderer.setSize(
        document.body.clientWidth,
        document.body.clientHeight
    );
    document.getElementById("container").appendChild(renderer.domElement);

    // 交互控制
    var contrlos = new THREE.OrbitControls(camera,renderer.domElement);

    //  加载模型
    loadCarModel();

    //加灯光
    addLight();

    loop();

    

}

function loop(){
    requestAnimationFrame(loop);
    renderer.render(scene,camera);
}

function loadCarModel(){
    const loader = new THREE.GLTFLoader();
    loader.load(
        './images/model.gltf',
        function(gltf){ // 什么是回调函数？
            scene.add(gltf.scene);
        },
        function(xhr){
            console.log(xhr.loaded / xhr.total * 100 + '% loaded');
        },
        function(error){
            console.log('a error')
        }
    )
}

function addLight(){

    var ambientLight = new THREE.AmbientLight(0x5b5b5b); // 带颜色的灯光
    ambientLight.intensity = 1.2;
    scene.add(ambientLight);
}

window.onload = init;